package bsh.dyn;

/**
 * {@link ActualParameter} and {@link DeclaredParameterType} to wrap primitive types
 * 
 * @author takeshi
 */
public class PrimitiveParameter implements ActualParameter, DeclaredParameterType {

    private final Class<?> javaType;

    /**
     * C'tor
     * 
     * @param javaType {@link Class}
     */
    public PrimitiveParameter(Class<?> javaType) {
        super();
        this.javaType = javaType;
        if (!this.javaType.isPrimitive()) {
            throw new IllegalArgumentException("not a primitive: " + javaType);
        }
    }

    /**
     * {@inheritDoc}
     */
    public boolean isPrimitive() {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isPrimitiveWrapper() {
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public Class<?> getJavaType() {
        return this.javaType;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isNull() {
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public ActualParameter[] getDeclaredInterfaces() {
        return new ActualParameter[0];
    }

    /**
     * {@inheritDoc}
     */
    public ActualParameter getSuperType() {
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public boolean acceptsNull() {
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public DeclaredParameterType getComponentType() {
        return null;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isArray() {
        return false;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isSuperClassOf(Parameter another) {
        return this.javaType.equals(another.getJavaType());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.javaType == null) ? 0 : this.javaType.hashCode());
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        PrimitiveParameter other = (PrimitiveParameter) obj;
        if (this.javaType == null) {
            if (other.javaType != null) {
                return false;
            }
        } else if (!this.javaType.equals(other.javaType)) {
            return false;
        }
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "PrimitiveParameter [javaType=" + javaType + "]";
    }

}
